<?php

namespace app\admin\controller;

use app\admin\model\User;
use app\common\controller\Backend;
use think\Db;

/**
 * 充值提现管理
 *
 * @icon fa fa-circle-o
 */
class RechargeWithdrawal extends Backend
{

    /**
     * RechargeWithdrawal模型对象
     * @var \app\common\model\RechargeWithdrawal
     */
    protected $model = null;

    protected $searchFields = 'id,user.mobile';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\RechargeWithdrawal;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','user_name','money','status','msg','status_time','createtime']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['mobile','bank_num','bank_sub_name','bank_name','real_name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->assignconfig('type',(int)$this->request->param('type'));
        return $this->view->fetch();
    }



    //审核
    public function examine($id = null){
        /** @var \app\common\model\RechargeWithdrawal $info */
        $info = $this->model->find($id);
        if( !$this->request->isAjax() ){
            $this->assign('row',$info);
            return $this->view->fetch();
        }

        if( empty($info) ){
            $this->error('没有找到审核信息');
        }
        if( $info->status != 1 ){
            $this->error('已审核');
        }

        $status = $this->request->param('status');
        $msg = $this->request->param('msg');
        if( !in_array($status,[2,3]) ){
            $this->error('参数错误');
        }
        if( $status == 3 &&  empty($msg) ){
            $this->error('驳回必须填写驳回原因');
        }
        /** @var User $userInfo */
        $userInfo = $info->user;
        Db::startTrans();
        try {
            $info->status = $status;
            $info->msg = $msg;
            $info->status_time = time();
            $info->save();
            //如果充值审核通过则增加余额
            if( $info->type == 1 && $status == 2 ){
                //增加累积充值
                $userInfo->sum_recharge_money  += $info->money;
                $userInfo->save();
                updateMoney($userInfo,$info->money,'充值');
            }
            //如果提现驳回则需要返回对应金额
            if( $info->type == 2 && $status == 3){
                //updateMoney($userInfo,$info->money,'提现驳回');
            }
            //提现审核成功则增加对应的累计提现金额
            if( $info->type == 2 && $status == 2 ){
                $userInfo->sum_withdrawal_money += $info->money;
                updateMoney($userInfo,intval($info->money)*-1,'提现');
                $userInfo->save();
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error('审核失败：'.$e->getMessage());
        }

        $this->success('审核成功');

    }
}
